package greetings

import (
    "errors"
    "fmt"
		"math/rand"
		"time"
)

// Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return "www", errors.New("empty name")
    }

    // If a name was received, return a value that embeds the name
    // in a greeting message.
    message := fmt.Sprintf(randomFormat(), name)
    return message, nil
}


// Hellos returns a map that associates each of the named people
// with a greeting message.
func Hellos(names []string) (map[string]string, error) {
	// A map to associate names with messages.
	messages := make(map[string]string)
	// Loop through the received slice of names, calling
	// the Hello function to get a message for each name.
	for _, name := range names {
			message, err := Hello(name)
			if err != nil {
					return nil, err
			}
			// In the map, associate the retrieved message with
			// the name.
			messages[name] = message
	}
	return messages, nil
}

// init sets initial values for variables used in the function.
func init() {
	rand.Seed(time.Now().UnixNano())
}

// randomFormat returns one of a set of greeting messages. The returned
// message is selected at random.
func randomFormat() string {
	formats := []string{
		"hi, %v. Welcome!\n",
		"Great to see you, %v\n",
		"Hail, %v! Well met!\n",
	}
	// Return a randomly selected message format by specifying
	// a random index for the slice of formats.
	
	le := len(formats)
	ind := rand.Intn(le)
	return formats[ind]
}